#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long int64;

const double eps = 1e-7;

struct circle_t
{
	double x, y, r;
};

circle_t c[100000];
int n;

bool by_right_end(const circle_t &a, const circle_t &b)
{
	return a.x + a.r < b.x + b.r;
}

double dist(const circle_t &a, const circle_t &b)
{
	return hypot(a.x - b.x, a.y - b.y) - a.r - b.r;
}

int	main()
{
	int T;
	cin >> T;

	while (T--)
	{
		cin >> n;
		double alpha = rand();
		double cosA = cos(alpha);
		double sinA = sin(alpha);
		for (int i = 0; i < n; ++i) 
		{
			scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].r);
			double u = c[i].x * cosA - c[i].y * sinA;
			double v = c[i].x * sinA + c[i].y * cosA;
			c[i].x = u;
			c[i].y = v;
		}

		sort(c, c + n, by_right_end);

		double answer = dist(c[n - 2], c[n - 1]);
		int m = n - 1;
		for (int i = n - 3; i >= 0; --i)
		{
			for (int j = i + 1; j < m; ++j)
			{
				while (j < m && c[j].x - c[j].r > c[i].x + c[i].r + answer)
				{
					swap(c[j], c[m]);
					--m;
				}
				if (j < m)
				{
					answer = min(answer, dist(c[i], c[j]));
				}
			}
		}
		printf("%.6lf\n", answer);
	}
	return 0;
}
